package main

import (
	"fmt"
	"github.com/gomodule/redigo/redis"
	"reflect"
)

var pool *redis.Pool

type Account struct{
	Id int `json:"id"`
	Name string `json:"name"`
	Age int	`json:"age"`
	Country string `json:"country"`
	Password string `json:"password"`
	FriendsList []FriendsInfo `json:"friendsList"`
}

type FriendsInfo struct{
	Acc int `json:"acc"`
	Message []string `json:"message"`
}

func init(){
	pool = &redis.Pool{
		MaxIdle : 10,
		MaxActive : 0,
		IdleTimeout : 100,
		Dial : func()(redis.Conn, error){
			return redis.Dial("tcp", "127.0.0.1:6379")
		},
	}

}

func main(){

	var dataList []map[string]interface{}

	for k:=1; k<=5; k++ {
		data := make(map[string]interface{})
		data["id"] = k
		data["name"] = "Curry"
		data["age"] = 22
		data["country"] = "中国"
		data["password"] = "123456"
		//data["friendsList"] = make([]map[string]interface{}, 1)
		// friendsInfo := make(map[string]interface{})
		// friendsInfo["acc"] = 2
		// friendsInfo["message"] = "小伙子，牛逼啊！"
		//var acclistMap []map[string]interface{}
		var acclistMap []interface{}
		for i := 2; i < 22; i++ {
			accMap := make(map[string]interface{})
			accMap["acc"] = i
			// acclistMap = make([]map[string]interface{}, 1)
			// acclistMap[0] = make(map[string]interface{})
			// acclistMap[0]["acc"] = 2
			var mesList []string
			//mesList = make([]string, 1)
			//mesList[0] = "小伙子，牛逼啊！"
			mesList = append(mesList, "小伙子，牛逼啊！", "关你屁事！", "滚", "JAVA是世界上最好的语言", "golang天下第一")
			//acclistMap[0]["message"] = mesList
			// acclistMap[0]["message"] = make([]string, 1)
			// acclistMap[0]["message"][0] = "小伙子，牛逼啊！"
			accMap["message"] = mesList
			//data["friendsList"] = acclistMap
			// data["friendsList"][0] = make(map[string]interface{})
			//data["friendsList"] = append(data["friendsList"], friendsInfo)
			acclistMap = append(acclistMap, accMap)
		}
		data["friendsList"] = acclistMap
		//jsonData, err:= json.Marshal(data)
		//fmt.Println(string(jsonData))

		//if err != nil{
		//	fmt.Println(err)
		//	return
		//}
		dataList = append(dataList, data)
	}

	poolConn := pool.Get()
	_,pErr := poolConn.Do("HSet", "acountList", 318464706073329664, dataList)
	if pErr != nil{
		fmt.Println("HSet ERROR:",pErr)
		return
	}

	res, pdErr := redis.String(poolConn.Do("HGET", "acountList",318464706073329664))
	if pdErr != nil{
		fmt.Println("HGETALL ERROR:",pdErr)
		return
	}
	fmt.Println("acountList：", res)

	_, err := poolConn.Do("LPUSH", "list1", "ele1","ele2","ele3")
	if err != nil {
		fmt.Println("redis mset error:", err)
	}
	res, err1 := redis.String(poolConn.Do("LPOP", "list1"))
	if err1 != nil {
		fmt.Println("redis POP error:", err1)
	} else {
		res_type := reflect.TypeOf(res)
		fmt.Printf("res type : %s \n", res_type)
		fmt.Printf("res  : %s \n", res)
	}

	//poolConn := pool.Get()
	//_,pErr := poolConn.Do("HSet", "acount", data["id"], data)
	//if pErr != nil{
	//	fmt.Println("HSet ERROR:",pErr)
	//	return
	//}
	//
	//res, pdErr := redis.String(poolConn.Do("HGET", "acount",1))
	//if pdErr != nil{
	//	fmt.Println("HGETALL ERROR:",pdErr)
	//	return
	//}
	//fmt.Println(res)
	//
	//_,pErr1 := poolConn.Do("DEL", "acount", data["id"], data)
	//if pErr1 != nil{
	//	fmt.Println("HSet ERROR:",pErr1)
	//	return
	//}
	//
	//res, pdErr1 := redis.String(poolConn.Do("HGET", "acount",1))
	//if pdErr1 != nil{
	//	fmt.Println("HGETALL ERROR1:",pdErr1)
	//	return
	//}
	//fmt.Println(res)

	//acc := Account{}
	//jErr := json.Unmarshal([]byte(res),&acc)
	//if jErr != nil{
	//	fmt.Println(jErr)
	//	return
	//}
	//fmt.Println(acc.Name)
	//fmt.Println(acc.Age)
	//fmt.Println(acc.Country)
	//fmt.Println(acc.Password)
	//fmt.Println(acc.Id)
	//for _, v := range acc.FriendsList{
	//	fmt.Println(v.Acc)
	//	for _, mv := range v.Message{
	//		fmt.Println(mv)
	//	}
	//}

	//if acc.Id == 1 && acc.Password == "123456"{
	//	fmt.Println("登陆成功！")
	//}else{
	//	fmt.Println("账号或密码错误")
	//}
}
